package com.oceansoft.mobile.econsole.modules.weixin.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.oceansoft.mobile.econsole.common.constant.ConfigInfo;
import com.oceansoft.mobile.econsole.common.util.HttpClientUtil;
import com.oceansoft.mobile.econsole.modules.base.entity.ApiPager;
import com.oceansoft.mobile.econsole.modules.base.entity.Query;
import com.oceansoft.mobile.econsole.modules.base.entity.Result;
import com.oceansoft.mobile.econsole.modules.weixin.entity.LostProperty;
import com.oceansoft.mobile.econsole.modules.weixin.persistence.ILostPropertyMapper;
import com.oceansoft.mobile.econsole.modules.weixin.service.ILostPropertyService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@Service
@Transactional
public class LostPropertyServiceImpl implements ILostPropertyService {
    @Resource
    private ILostPropertyMapper lostPropertyMapper;

    @Resource
    private ConfigInfo configInfo;

    public ApiPager<LostProperty> loadLostPropertiesByPage(Query query) {
        PageHelper.startPage(query.getPageNum(), query.getNumPerPage(), true);
        Page<LostProperty> page = (Page<LostProperty>) lostPropertyMapper.loadLostPropertiesByCondition(query);
        ApiPager<LostProperty> apiPager = new ApiPager<LostProperty>(query.getNumPerPage(), query.getPageNum(), page.getTotal(), page.getResult());
        return apiPager;
    }

    public Result findLostPropertyById(String guid) {

        String url = configInfo.getServiceInterfaceAddress() + "/apphub/lostproperty/" + guid;
        String res = null;
        try {
            res = HttpClientUtil.doGet(url);
        } catch (IOException e) {
        }

        JSONObject object = JSON.parseObject(res);
        Preconditions.checkArgument(object.getBooleanValue("succ"), "获取接口数据返回失败");

        LostProperty data = object.getObject("data", LostProperty.class);
        return new Result(Result.SUCC, data);
    }

    @Override
    public ApiPager<LostProperty> loadLostProperties(Query query) {
        String res = null;
        int pageNum = query.getPageNum();
        int numPerPage = query.getNumPerPage();
        String keywords = query.getKeywords();

        String url = configInfo.getServiceInterfaceAddress() + "/apphub/lostproperty/page?pi=" + pageNum + "&ps=" + numPerPage;
        url = !Strings.isNullOrEmpty(keywords) ? url + "&lostname=" + keywords : url;
        try {
            res = HttpClientUtil.doGet(url);
        } catch (IOException e) {
            return new ApiPager();
        }
        JSONObject object = JSON.parseObject(res);
        Preconditions.checkArgument(object.getBooleanValue("succ"), "获取接口数据返回失败");

        JSONObject jsonObject = object.getJSONObject("data");
        int pageSize = jsonObject.getIntValue("pageSize");
        int pageIndex = jsonObject.getIntValue("pageIndex");
        long totalRow = jsonObject.getLongValue("totalRow");
        List<LostProperty> list = JSON.parseArray(jsonObject.getString("list"), LostProperty.class);

        ApiPager pager = new ApiPager(pageSize, pageIndex, totalRow, list);
        return pager;
    }

    //	public Result loadLostPropertyByPage(Query query){
//		HashMap<String, Object> result = new HashMap<String, Object>(2);
//		int total = lostPropertyMapper.loadLostPropertiesByCondition(query).size();
//		// 加载新闻列表数据
//		PageHelper.startPage(query.getPageNum(), Query.PAGE_SIZE, true);
//		Page<LostProperty> page = (Page<LostProperty>) lostPropertyMapper.loadLostPropertiesByCondition(query);
//		List<LostProperty> list = page.getResult();
//		final ApiPager<LostProperty> pager = new ApiPager<LostProperty>(Constant.API_PAGE_SIZE, query.getPageNum(), total, list);
//		result.put("body", pager);
//		if (list.size() > 0) {
//			return new Result(result);
//		} else {
//			return new Result(Result.FAILURE, "暂无数据");
//		}
//	}

}
